package com.caiyi.financial.nirvana.sms.gateway.http;

import com.alibaba.fastjson.JSON;
import com.caiyi.financial.nirvana.sms.enums.SmsMsgStatusEnum;
import com.caiyi.financial.nirvana.sms.queue.NotifyQueueEntity;
import com.caiyi.financial.nirvana.sms.queue.ReceiveMsgQueueEntity;
import com.caiyi.financial.nirvana.sms.queue.SendQueueEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class HongLianGateway extends BaseGateway {
    private static final Logger logger = LoggerFactory.getLogger(HongLianGateway.class);

    @Override
    public void sendMessage(SendQueueEntity entity) {
        int status = SmsMsgStatusEnum.REQUEST_FAIL.index;
        String result = "";
        try {
            Map<String, String> params = buildParam(entity);
            logger.info("params : {}", params);
            String response = doPost(info.getServerAddr(), params);
            if (response == null) {
                result = "网关请求超时";
                return;
            }
            logger.info("HongLianGateway send url : {}, params : {}, result : {}", info.getServerAddr(), params, response);
            if ("00".equals(response)) {
                status = SmsMsgStatusEnum.REQUEST_SUCCESS.index;
                result = response;
            } else {
                result = response;
            }
        } catch (Exception e) {
            logger.error("HongLianGateway error ! ", e);
        } finally {
            onSubmitResponse(status, entity.getMessageId(), result);
        }
    }

    public static void main(String[] args) throws Exception {
        Map<String, String> body = new HashMap<>();
        body.put("username", "bst1");
        body.put("password", "Bst493402");
        body.put("phone", "18117351521");
        body.put("message", new String("【展奎科技】你的验证码为:123456".getBytes(), "gbk"));
        body.put("epid", "125167");
        body.put("linkid", System.currentTimeMillis() + "");
        body.put("subcode", "");
        String response = doPost("http://114.255.71.158:8061", body);
        logger.info("body : {}, response is {}", body, response);
    }

    private Map<String, String> buildParam(SendQueueEntity entity) {
        Map<String, String> body = new HashMap<>();
        try {
            body.put("username", info.getAccount());
            body.put("password", info.getPassword());
            body.put("phone", entity.getPhone());
            body.put("message", URLEncoder.encode(entity.getContent(), "gb2312"));
            body.put("epid", info.getServerPort() + "");
            body.put("linkid", entity.getMessageId());
            body.put("subcode", "");
        } catch (Exception e) {
        }
        return body;
    }

    @Override
    public boolean notifyStatus(NotifyQueueEntity entity) {
        try {
            String param = entity.getParam();
            logger.info("HongLianGateway notifyStatus param : {}", param);
            Map<String, String> paramMap = JSON.parseObject(param, Map.class);
            int status = SmsMsgStatusEnum.SEND_FAIL.index;
            String messageId = paramMap.get("FlinkID");
            String code = paramMap.get("FReportCode");
            if ("0".equals(code) || "DELIVRD".equals(code)) {
                status = SmsMsgStatusEnum.SEND_SUCCESS.index;
            }
            String result = paramMap.get("FAckStatus");
            if (StringUtils.isBlank(messageId)) {
                logger.warn("HongLianGateway notifyStatus messageId is null params : {}", param);
            }
            receiveReport(status, messageId, result);
            return true;
        } catch (Exception e) {
            logger.error("HongLianGateway notifyStatus error ! ", e);
            return false;
        }
    }

    @Override
    public boolean receiveMsg(ReceiveMsgQueueEntity entity) {
        return true;
    }

    @Override
    public int queryBalance() {
        return 0;
    }
}
